package com.example.hospital.controller;

import com.example.hospital.dto.MedicalrecordQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Medicalrecord;
import com.example.hospital.service.MedicalrecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("medicalrecord")
@Tag(name = "Medical Record", description = "病历相关接口")
public class MedicalrecordController {
    @Resource
    private MedicalrecordService medicalrecordService;

    /**
     * 新增病历
     *
     * @param medicalrecord
     * @return medicalrecord id or error
     */
    @Operation(
            summary = "新增病历",
            description = "创建一条新的病历记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回病历ID"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result<Integer> addMedicalrecord(@RequestBody Medicalrecord medicalrecord) {
        boolean saved = medicalrecordService.save(medicalrecord);
        return saved ? Result.success("添加成功", medicalrecord.getRecordId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除病历
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除病历",
            description = "通过病历ID删除指定的病历记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "病历未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteMedicalrecord(@PathVariable("id") Integer id) {
        boolean removed = medicalrecordService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改病历信息
     *
     * @param medicalrecord
     * @return
     */
    @Operation(
            summary = "修改病历信息",
            description = "通过传递新的病历信息更新现有病历",
            responses = {
                    @ApiResponse(responseCode = "200", description = "修改成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updateMedicalrecord(@RequestBody Medicalrecord medicalrecord) {
        boolean updated = medicalrecordService.updateById(medicalrecord);
        return updated ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id查询病历
     *
     * @param id
     * @return medicalrecord or error
     */
    @Operation(
            summary = "根据id查询病历",
            description = "通过病历ID查询病历信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "病历未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Medicalrecord> getMedicalrecord(@PathVariable("id") Integer id) {
        Medicalrecord medicalrecord = medicalrecordService.getById(id);
        return medicalrecord != null ? Result.success("查询成功", medicalrecord) : Result.fail("查询失败");
    }

    /**
     * 查询所有病历
     *
     * @return medicalrecord list or error
     */
    @Operation(
            summary = "查询所有病历",
            description = "获取所有病历记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @GetMapping
    public Result<List<Medicalrecord>> listMedicalrecords() {
        return Result.success(medicalrecordService.list());
    }

    /**
     * 根据患者id查询病历
     *
     * @param patientId
     * @return medicalrecord list or error
     */
    @Operation(
            summary = "根据患者id查询病历",
            description = "通过患者ID查询病历信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "病历未找到")
            }
    )
    @GetMapping("/patient/{patientId}")
    public Result<List<Medicalrecord>> listMedicalrecordsByPatientId(@PathVariable("patientId") Integer patientId) {
        List<Medicalrecord> medicalrecords = medicalrecordService.lambdaQuery().eq(Medicalrecord::getPatientId, patientId).list();
        return medicalrecords != null ? Result.success("查询成功", medicalrecords) : Result.fail("查询失败");
    }

    /**
     * 条件查询病历
     *
     * @param medicalrecordQuery
     * @return medicalrecord list
     */
    @Operation(
            summary = "条件查询病历",
            description = "通过条件查询病历信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "病历未找到")
            }
    )
    @PostMapping("/query")
    public Result<List<Medicalrecord>> queryMedicalrecord(@RequestBody MedicalrecordQuery medicalrecordQuery) {
        List<Medicalrecord> medicalrecords = medicalrecordService.listMedicalrecords(medicalrecordQuery);
        return Result.success("查询成功", medicalrecords);
    }
}
